import { Game } from "@sapi-game/main";
import { showWereWolfForm, WereWolfFormOptions } from "@/werewolf/func";
import { WereWolfModule } from "@/werewolf/module";
import { WereWolfHunterPhase } from "@/werewolf/phases/hunter";
import { WereWolfStatus } from "@/werewolf/player";

export class HunterTool extends WereWolfModule.Component<
    unknown,
    WereWolfHunterPhase
> {
    get hunter() {
        return this.state.hunter;
    }

    override onAttach(): void {
        this.subscribe(
            Game.events.itemUse,
            (t) => {
                const p = this.hunter?.player;
                if (t.source.id == p?.id) {
                    this.showForm();
                }
            },
            { itemId: "werewolf:hunter_gun" }
        );
    }

    hunterFormData: WereWolfFormOptions = {
        title: "猎人菜单",
        body: "请选择你要带走的玩家",
        getPlayers: () =>
            this.context.players.filter((p) => p.status == WereWolfStatus.Live),
        onSelect: (player, selected) => {
            if (!this.isAttached) return;
            selected.status = WereWolfStatus.justDead;
            player.sendMessage(`你带走了[${selected.no}号]${selected.name}`);
            this.context.players.sendMessage("猎人带走了:§e" + selected.name);
            this.state.next();
        },
    };

    showForm() {
        const hunter = this.hunter;
        if (!hunter?.player) return;
        showWereWolfForm(this.hunterFormData, hunter);
    }
}
